package com.woan.api.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.lang.time.DateUtils;

import com.alibaba.druid.sql.visitor.functions.Now;
import com.woan.api.util.ConfigUtil;
import com.woan.api.util.DateJsonValueProcessor;
import com.woan.api.util.FuncDate;
import com.woan.api.util.PushAsync;
import com.woan.api.vo.VClientvideoList;
import com.woan.api.vo.VpositionList;
import com.woan.api.idao.SysAdmininfoIDAO;
import com.woan.api.idao.SysMessageIDAO;
import com.woan.api.idao.SysVideoBasicIDAO;
import com.woan.api.idao.SysVideoIDAO;
import com.woan.api.idao.ZbConfigIDAO;
import com.woan.api.model.SysAdmininfo;
import com.woan.api.model.SysMessage;
import com.woan.api.model.SysVideo;
import com.woan.api.model.SysVideoBasic;











import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;



public class VideoClientServices {
	private SysAdmininfoIDAO sysadmininfoiDAO;
	private SysVideoIDAO sysvideoiDAO;
	private SysMessageIDAO sysmessageiDAO;
	private SysVideoBasicIDAO sysvideobasiciDAO;
	private ZbConfigIDAO zbconfigiDAO;
	
	
	public ZbConfigIDAO getZbconfigiDAO() {
		return zbconfigiDAO;
	}
	public void setZbconfigiDAO(ZbConfigIDAO zbconfigiDAO) {
		this.zbconfigiDAO = zbconfigiDAO;
	}
	public SysAdmininfoIDAO getSysadmininfoiDAO() {
		return sysadmininfoiDAO;
	}
	public void setSysadmininfoiDAO(SysAdmininfoIDAO sysadmininfoiDAO) {
		this.sysadmininfoiDAO = sysadmininfoiDAO;
	}

	public SysVideoBasicIDAO getSysvideobasiciDAO() {
		return sysvideobasiciDAO;
	}
	public void setSysvideobasiciDAO(SysVideoBasicIDAO sysvideobasiciDAO) {
		this.sysvideobasiciDAO = sysvideobasiciDAO;
	}
	public SysVideoIDAO getSysvideoiDAO() {
		return sysvideoiDAO;
	}
	public void setSysvideoiDAO(SysVideoIDAO sysvideoiDAO) {
		this.sysvideoiDAO = sysvideoiDAO;
	}
	
	public SysMessageIDAO getSysmessageiDAO() {
		return sysmessageiDAO;
	}
	public void setSysmessageiDAO(SysMessageIDAO sysmessageiDAO) {
		this.sysmessageiDAO = sysmessageiDAO;
	}
	public SysVideoBasic getBasicVideoByVid(String vid){
		
		SysVideoBasic sysVideoBasic = sysvideobasiciDAO.get(vid);
		return sysVideoBasic;
	}
	public String editVideos(String rid,String uid,String username,String vid,String title,String address,String longitude,String latitude,String detail,String countrycode,String countryname,String province,String city,String counties){
		String flag="0";
		JSONObject json=new JSONObject();
		flag = sysvideobasiciDAO.EditVideos(rid,uid,username,vid, title,address,longitude,latitude,detail,countrycode,countryname,province,city,counties);
		return flag;
	}
	public List<VClientvideoList> listLiveVideos(String rid,String type,Integer beginIndex,Integer endIndex)
	{
		List<VClientvideoList> videolist =  new ArrayList<VClientvideoList>();
		
		videolist = sysvideoiDAO.FindVCLiveVideoByGroup(rid,type,beginIndex, endIndex);
		//判断是否需要更改视频地址
		String zhuan_ip_flag = ConfigUtil.get("zhuan_ip_flag");
		if (zhuan_ip_flag.endsWith("true")) {
			tranIp(videolist);
		}
		return videolist;
	}
	
	public List<VClientvideoList> listHistoryVideos(String rid,String type,Integer beginIndex,Integer endIndex)
	{
		List<VClientvideoList> videolist =  new ArrayList<VClientvideoList>();
		
		videolist = sysvideoiDAO.FindVCHistoryVideoByGroup(rid,type, beginIndex, endIndex);
		//判断是否需要更改视频地址
		String zhuan_ip_flag = ConfigUtil.get("zhuan_ip_flag");
		if (zhuan_ip_flag.endsWith("true")) {
			tranIp(videolist);
		}
		return videolist;
	}
	public List<VClientvideoList> listMyVideos(String uid,String type,Integer beginIndex,Integer endIndex){
		List<VClientvideoList> videolist = new ArrayList<VClientvideoList>();
		videolist = sysvideoiDAO.FindVMyVideoByUid(uid,type, beginIndex, endIndex);
		//判断是否需要更改视频地址
		String zhuan_ip_flag = ConfigUtil.get("zhuan_ip_flag");
		if (zhuan_ip_flag.endsWith("true")) {
			tranIp(videolist);
		}
		return videolist;
	}
	public SysVideo listOneVideoinfo(String vid,String type){
		SysVideo video = null;
		video = sysvideoiDAO.FindVideoByVidAndType(vid,type);
		//判断是否需要更改视频地址
		String zhuan_ip_flag = ConfigUtil.get("zhuan_ip_flag");
		if (zhuan_ip_flag.endsWith("true")) {
			if (video!=null) {
				String neiwang_ip = ConfigUtil.get("neiwang_ip");
				String videoPicture = video.getPicture();
				if (videoPicture!=null&&videoPicture.length()>1) {
					String substring = videoPicture.substring(videoPicture.indexOf("/", 8));
					String newPathString=neiwang_ip+substring;
					video.setPicture(newPathString);
				}
				String videoPath = video.getVideoPath();
				if (videoPath!=null&&videoPath.length()>1) {
					String substring = videoPath.substring(videoPath.indexOf("/", 8));
					String newPathString=neiwang_ip+substring;
					video.setVideoPath(newPathString);
				}
			}
		}
		return video;
	}
	public List<SysVideo> listVideoinfo(String vid,String type){
		List<SysVideo> video = null;
		video = sysvideoiDAO.FindAllVideoByVidAndType(vid,type);
		//判断是否需要更改视频地址
		String zhuan_ip_flag = ConfigUtil.get("zhuan_ip_flag");
		if (zhuan_ip_flag.endsWith("true")) {
			if (video.size()>0) {
				String neiwang_ip = ConfigUtil.get("neiwang_ip");
				for (SysVideo sysVideo : video) {
					String videoPicture = sysVideo.getPicture();
					if (videoPicture!=null&&videoPicture.length()>1) {
						String substring = videoPicture.substring(videoPicture.indexOf("/", 8));
						String newPathString=neiwang_ip+substring;
						sysVideo.setPicture(newPathString);
					}
				}
			}
		}
//		if(video!=null)
//		{
//			ResourceBundle rb_config = ResourceBundle.getBundle("config");
//			String videorealpath = rb_config.getString("videorealpath");
//			String oldaddress="";
//			if(type.equals("android"))
//				oldaddress = video.getVideoPath().substring(0,video.getVideoPath().indexOf("/live")+1);
//			if(type.equals("ios"))
//				oldaddress = video.getVideoPath().substring(0,video.getVideoPath().indexOf("/mobile")+1);
//			video.setVideoPath(video.getVideoPath().replaceAll(oldaddress, videorealpath));
//		}
		return video;
	}
	public int listHistoryVideosCount(String rid,String type){
		
		return sysvideoiDAO.FindHistoryVideoCountByGroup(rid,type);
	}
	public int listMyVideosCount(String uid,String type){

		return sysvideoiDAO.FindMyVideoCountByUid(uid,type);
	}
	public List<VpositionList> listVideoPostion(String rid,String type){
		List<VpositionList> postion = null;
		postion = sysvideoiDAO.FindVVideoPostion(rid,type);
		return postion;
	}
	//视频服务器post视频信息接口---插入新视频
	public void insertVideo(SysVideo sysVideo,String rid,String groupname,String username)
	{
		sysvideoiDAO.InsertVideo(sysVideo);
	}
	public void pushMessageFlv(SysVideo sysVideo,String rid,String groupname,String username)
	{
		//if(sysVideo.getVideoType().equals("android"))
		//{
			//String message = zbconfigiDAO.QueryConfigPushOrNot(rid, username);
			//System.out.println("777777777777777777777777777777777777777");
			//if(message.equals("1"))
			//{
				List Androidpushlist = sysmessageiDAO.Getpushlist(rid, username, "android");	
				if(Androidpushlist.size()>0)
				{		
					PushAsync sendAsync = new PushAsync(groupname,username,sysVideo.getVid(),Androidpushlist,"android","zbstart");
					sendAsync.send();	
				}
			//}
			
		//}
	}
	//更新视频状态-----拍摄之前首先将之前的未结束的视频结束
	public void updateVideoState(String uid, String vid){
		System.out.println("更新视频状态-----拍摄之前首先将之前的未结束的视频结束");
		try {
			sysvideoiDAO.UpdateVideoState(uid,vid);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//视频服务器post视频信息接口---视频存档
	public void updateVideo(SysVideo sysVideo,String rid,String groupname,String username)
	{	
		sysvideoiDAO.UpdateVideo(sysVideo);
	}
	public void pushMessageM3u8(SysVideo sysVideo,String rid,String groupname,String username)
	{
		if(sysVideo.getVideoType().equals("ios") && sysVideo.getStatus()==0)//状态为0的时发送push，如果m3u8停止则不发
		{
			//String message = zbconfigiDAO.QueryConfigPushOrNot(rid, username);
			System.out.println(sysVideo.getVideoType()+"888888888888888888888888888888888888888888");
			//if(message.equals("1"))
			//{
				List Iospushlist = sysmessageiDAO.Getpushlist(rid, username, "ios");
				if(Iospushlist.size()>0)
				{
					PushAsync sendAsync = new PushAsync(groupname,username,sysVideo.getVid(),Iospushlist,"ios","zbstart");
					sendAsync.send();
				}
			//}
		}
	}
	public void insertVideo_flv(SysVideo sysVideo, SysAdmininfo user) {
		sysvideoiDAO.InsertVideo_flv(sysVideo,user);
		
	}
	
	//插入一条视频基本信息
	public void inserVideoBasic(SysAdmininfo admin,String vs_id){
		try {
			
			SysVideoBasic videoBasic = new SysVideoBasic();
			videoBasic.setVid(vs_id);
			videoBasic.setCreateTime(FuncDate.FindCurSysTime1());
			videoBasic.setUserName(admin.getUserName());
			videoBasic.setUid(admin.getUid());
			videoBasic.setRid(admin.getSysRole().getRid());
			videoBasic.setTitle("摄像头直播");
			videoBasic.setDetail("暂无描述");
			//插入视频信息成功。未发生任何异常
			
			sysvideobasiciDAO.saveVideoBasic(videoBasic);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 格式化内网ip
	 */
	private void tranIp(List<VClientvideoList> videolist) {
		String neiwang_ip = ConfigUtil.get("neiwang_ip");
		if (videolist.size()>0) {
			for (VClientvideoList video : videolist) {
				String videoPath = video.getVideoPath();
				if (videoPath!=null&&videoPath.length()>1) {
					String substring = videoPath.substring(videoPath.indexOf("/", 8));
					String newPathString=neiwang_ip+substring;
					video.setVideoPath(newPathString);
				}
				String videoPicture = video.getPicture();
				if (videoPicture!=null&&videoPicture.length()>1) {
					String substring = videoPicture.substring(videoPicture.indexOf("/", 8));
					String newPathString=neiwang_ip+substring;
					video.setPicture(newPathString);
				}
			}
		}
	}
}
